package com.apes.scm.rbac.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.query.ApesDao;
import com.apes.framework.util.MapUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by lishikun on 2021/4/13 17:15
 */
@Component
public class RbacDao extends ApesDao {
    public JSONArray queryRoleIdByMenuId(JSONObject param) {
        String sSQL = "         select ROLE_ID\n" +
                "           from AUT_ROLE_OPERATION\n" +
                "          where menu_id = :menuId\n" +
                "          group by ROLE_ID";
        return toJSONArray(this.find(sSQL, param));
    }

    public JSONArray queryPostIdByRoleId(JSONObject param) {
        String sSQL = "select POST_ID from aut_role_posts where role_id = :roleId";
        return toJSONArray(this.find(sSQL, param));
    }

    public JSONArray findAuthItemByMenu(JSONObject param) {
        String sSQL = " SELECT ROLE_ID,\n" +
                "        (SELECT NAME FROM AUT_ROLE WHERE ID = T.ROLE_ID) AS ROLE_NAME,\n" +
                "        TO_CHAR(WM_CONCAT((SELECT NAME\n" +
                "            FROM AUT_OPERATIONS\n" +
                "           WHERE OPERATION_ID = T.OPERATION_ID\n" +
                "             AND MENU_ID = T.MENU_ID))) AS OPERATIONS_NAME, MENU_ID,\n" +
                "        (SELECT NAME FROM AUT_MENUS WHERE ID = T.MENU_ID) AS MENU_NAME\n" +
                "   FROM AUT_ROLE_OPERATION T\n" +
                "  WHERE 1 = 1\n" +
                "  AND MENU_ID = :menuId" +
                "  GROUP BY ROLE_ID, MENU_ID";
        return toJSONArray(this.find(sSQL, param));
    }

    public List<Map> findTreeDataByGroupAndTerminalAndDAndDevelopUsers(Map<String, Object> map) {
        JSONObject param = new JSONObject();
        param.put("group", map.get("group"));
        param.put("terminal", map.get("terminal"));
        String sSQL = "SELECT T.ID, T.NAME, T.PATH, T.VALID,t.sort_code,\n" +
                "       (SELECT DESCRIPTION FROM MAS_ENUMERATION A WHERE A.ID = T.GROUP_TYPE) AS GROUP_TYPE,\n" +
                "       (SELECT DESCRIPTION FROM MAS_ENUMERATION A WHERE A.ID = T.TERMINAL_TYPE) AS TERMINAL_TYPE,\n" +
                "       LEVEL \n" +
                "  FROM AUT_MENUS T \n" +
                "  START WITH T.GROUP_TYPE = :group \n" +
                "        AND T.TERMINAL_TYPE = :terminal \n" ;
        if (map.get("developUsers") != null) {
            JSONArray developUsers = (JSONArray) map.get("developUsers");
            for (Object o : developUsers) {
                String s = (String) o;
                sSQL += "AND DEVELOP_USERS LIKE '%" +s +"%'" ;
            }
        }
        sSQL += "  CONNECT BY PRIOR T.PARENT_ID = T.ID \n" +
                "  ORDER BY LEVEL, T.ID \n";
        JSONArray array = toJSONArray(this.find(sSQL, param));
        List<Map> collect = array.stream().map(o -> {
            JSONObject item = (JSONObject) o;
            return MapUtil.mapper("id", item.getString("ID"), "name", item.getString("NAME"), "path", item.getString("PATH"),
                    "valid", item.getString("VALID"), "groupType", item.getString("GROUP_TYPE"),
                    "sortCode", item.getString("SORT_CODE"));
        }).collect(Collectors.toList());
        return collect;
    }
}
